package cc.zy.base.hr.controller;


import cc.zy.base.common.annotation.ControllerEndpoint;
import cc.zy.base.common.controller.BaseController;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.exception.FebsException;
import cc.zy.base.common.utils.DateUtil;
import cc.zy.base.common.utils.FebsUtil;
import cc.zy.base.hr.entity.ScoreMapping;
import cc.zy.base.hr.service.IScoreMappingService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 积分规则
 * Controller
 *
 * @author Chenjunjie
 * @date  2022/3/9 11:03
 */
@Controller
@Slf4j
@Validated
@RequiredArgsConstructor
public class ScoreMappingController extends BaseController {

    @Resource
    private IScoreMappingService scoreMappingService;

    @GetMapping(FebsConstant.VIEW_HR_PREFIX + "scoreMapping")
    public String scoreMappingIndex(){

        return FebsUtil.hrView("scoreMapping/scoreMapping");
    }

    /**
     * 查找积分规则
     * @param request
     * @param scoreMapping
     * @return
     */

    @GetMapping("scoreMapping/list")
    @ResponseBody
    public FebsResponse scoreMappingList(QueryRequest request, ScoreMapping scoreMapping){

        log.debug("前台传来的对象是:"+scoreMapping);
        Map<String, Object> dataTable = getDataTable(scoreMappingService.findScoreMapping(request,scoreMapping));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 增加积分规则（前端暂时不需要此功能）
     * @param request
     * @param scoreMapping
     * @return
     */

    @PostMapping("scoreMapping/add")
    @ResponseBody
    @ControllerEndpoint(operation = "新增积分规则", exceptionMessage = "新增积分规则异常")
    public FebsResponse addScoreMapping(@Valid ScoreMapping scoreMapping) {

        log.debug("前台传来的对象是："+scoreMapping);
        scoreMapping.setCreateUserId(FebsUtil.getCurrentUser().getUserId());
        scoreMapping.setLastModifyUserId(FebsUtil.getCurrentUser().getUserId());
        scoreMappingService.createScoreMapping(scoreMapping);
        return new FebsResponse().success();
    }

    /**
     * 删除积分规则（前端暂时不需要此功能）
     * @param
     * @param
     * @return
     */

    @GetMapping("scoreMapping/delete/{scoreMappingIds}")
    @ControllerEndpoint(operation = "删除积分规则", exceptionMessage = "删除积分规则异常")
    @ResponseBody
    public FebsResponse deleteScoreMappings(@NotBlank(message = "{required}") @PathVariable String scoreMappingIds) {

        log.debug("前台传来的对象是"+scoreMappingIds);
        String[] ids = scoreMappingIds.split(StringPool.COMMA);
        scoreMappingService.deleteScoreMappings(ids);
        return new FebsResponse().success();
    }

    /**
     * 修改积分规则
     * @param scoreMapping
     * @param
     * @return
     */

    @PostMapping("scoreMapping/update")
    @ControllerEndpoint(operation = "修改积分规则", exceptionMessage = "修改积分规则异常")
    @ResponseBody
    public FebsResponse updateScoreMapping(@Valid ScoreMapping scoreMapping) {

        log.debug("修改规则id：" + scoreMapping.getId());
        if (scoreMapping.getId() == null) {
            throw new FebsException("积分规则ID为空");
        }
        scoreMapping.setLastModifyUserId(FebsUtil.getCurrentUser().getUserId());
        scoreMapping.setLastModifyDate(new Date());
        scoreMappingService.updateScoreMapping(scoreMapping);
        return new FebsResponse().success();
    }

    /**
     * 修改功能中，查询前台输入的最大值或最小值是否已经存在
     *
     * @param maxValue maxValue
     * @param minValue minValue
     * @return Boolean false为重复
     * true为不重复
     */
    @GetMapping("scoreMapping/isRepetitive")
    @ResponseBody
    public Boolean isRepetitive(BigDecimal maxValue,BigDecimal minValue) {
        return scoreMappingService.isRepetitive(maxValue,minValue);
    }


    /**
     * 打开详情页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "scoreMapping/detail/{id}")
    public String scoreMappingDetail(@PathVariable Integer id, Model model) {
        resolveScoreMappingModel(id, model, true);
        return FebsUtil.hrView("scoreMapping/scoreMappingDetail");
    }

    /**
     * 打开修改模态框，将对象传到修改页面实现回显
     * @param id
     * @param model
     * @return
     */

    @GetMapping(FebsConstant.VIEW_PREFIX + "scoreMapping/update/{id}")
    public String scoreMappingUpdate(@PathVariable Integer id, Model model) {
        resolveScoreMappingModel(id, model, false);
        return FebsUtil.hrView("scoreMapping/scoreMappingUpdate");
    }

    /**
     * 修改更新的数据格式,将数据放到页面里
     */

    private void resolveScoreMappingModel(Integer id, Model model, Boolean transform) {
        ScoreMapping scoreMapping = scoreMappingService.findById(id);
        if (scoreMapping.getCreateDate() != null) {
            model.addAttribute("createDate", DateUtil.getDateFormat(scoreMapping.getCreateDate(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
        if (scoreMapping.getLastModifyDate() != null) {
            model.addAttribute("lastModifyDate", DateUtil.getDateFormat(scoreMapping.getLastModifyDate(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
        model.addAttribute("scoreMapping", scoreMapping);
    }

    /**
     * 打开增加模态框，将对象传到修改页面实现回显（前端暂无此需求）
     * @param
     * @param
     * @return
     */

    @GetMapping(FebsConstant.VIEW_PREFIX + "scoreMapping/add")
    public String scoreMappingAdd() {
        return FebsUtil.hrView("scoreMapping/scoreMappingAdd");
    }
}
